﻿/*
* LuaHelper.cpp
*
* lua辅助类
*
* create date: 2014.4.18
* create by: leo
*
*/

#include "LuaHelper.h"

USING_NS_CC;

CLuaHelper& CLuaHelper::getInstance()
{
	static CLuaHelper instance;
	return instance;
}

int CLuaHelper::getHandler(const char* name)
{
	const CCString* val = mHandlers.valueForKey(name);
	if (NULL != val)
	{
		return val->intValue();
	}
	return -1;
}

void CLuaHelper::registerHandler(const char* name, LUA_FUNCTION handler)
{
	mHandlers.setObject(CCString::createWithFormat("%d", handler), name);
}

void CLuaHelper::loadFile(const char* name)
{
	CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
	const char* luaString = CFileReader::CreateStringFromFile(name);
	int res = pEngine->executeString(luaString);
	CC_SAFE_DELETE_ARRAY(luaString);
}

CLuaHelper::CLuaHelper()
{
	// prepare lua engine
	CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
	CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
}

CLuaHelper::~CLuaHelper()
{}
